<?php

include "fonctions.php";
head_html("Cr&eacute;ation cadavre exquis");
en_tete();
?>

	<div id="contenu">
		<?php
			menu_lateral_droit();
		?>
		<div id="texte">
		<?php
		vousetesici("Le cadavre exquis");
		echo "<h1>Jeu du cadavre exquis</h1>";
			db_connect();
			if(isset($_POST['partieVisible']) && isset($_POST['partieCache']) && isset($_POST['idExercice'])){
				if($_POST['partieCache'] != "" && $_POST['partieVisible'] != ""){ 
	
					$req_add ="INSERT INTO LMLP.phrases (partieVisible,partieCache,idCadavreExcquis,pseudo) VALUES ('".$_POST['partieVisible']."','".$_POST['partieCache']."',".$_POST['idExercice'].",'".$_SESSION['idUtil']."');";
					$resultAdd = mysql_query($req_add) or die (mysql_error());
					$_POST['id'] = $_POST['idExercice'];
				}
			}
				
			
			if(isset($_POST['id'])){
				$req ="SELECT cad.idExercice, cad.vitrine FROM LMLP.ateliers as at,LMLP.inscriptions as ins, LMLP.exercices_ateliers as exo, LMLP.cadavre_exquis as cad, LMLP.exercices as exer WHERE  ins.pseudo=\"".$_SESSION['idUtil']."\" and ins.nomAtelier = at.nomAtelier and exo.nomAtelier = at.nomAtelier and exo.idExercice = cad.idExercice and exo.idExercice =".$_POST['id']." and exo.idExercice = exer.idExercice and exer.etat=\"actif\" ;";
				
				$result = mysql_query($req) or die (mysql_error());
				$nb = mysql_num_rows($result);
				
				if($nb==1)
				{
					$id = 0;
					while ($row = mysql_fetch_assoc($result)) {						
						
						$req_cad = "SELECT * FROM LMLP.phrases as p WHERE p.idCadavreExcquis=".$row['idExercice']." ORDER BY idPhrase DESC LIMIT 1;";
						$resultCad = mysql_query($req_cad) or die (mysql_error());
						$nbCad = mysql_num_rows($resultCad);
						if($nbCad<2){
							if($nbCad==1){
								while ($rowCad = mysql_fetch_assoc($resultCad)) {
									echo "<h2>Voici la derni&egrave;re phrase &eacute;crite: </h2>";
									echo "<p>".$rowCad['partieVisible']."</p>";
	
								}
							}else if($nbCad==0){
								echo "<h2>A vous de commencer le jeu: </h2>";
							}
							echo "<h2>A votre tour:</h2>";
							echo "<form name=\"inscription\" method=\"post\" action=\"cadavre_exquis.php#fil\">";
							if(isset($_POST['partieVisible']) && isset($_POST['partieCache'])){
								if($_POST['partieVisible'] == "" && $_POST['idExercice'] !=""){
								 echo "<p class=\"red\">Veuillez saisir une phrase dans partie visible!!</p>";	
								}
								if($_POST['partieCache'] == "" && $_POST['idExercice'] !=""){
								 echo "<p class=\"red\">Veuillez saisir une phrase dans partie cach&eacute;e!!</p>";	
								}
							}
							echo "<h3>Partie Cach&eacute;</h3>";
							echo "<textarea type=\"text\" name=\"partieCache\" rows=\"4\" cols=\"80\" required></textarea>";
							echo "<h3>Partie visible</h3>";
							echo "<textarea type=\"text\" name=\"partieVisible\" rows=\"4\" cols=\"80\" required></textarea>";		
							echo "<input style=\"display:none;\"  type=\"text\" name=\"idExercice\" value=\"".$row['idExercice']."\">";
							echo "<br><input type=\"submit\" name=\"valider\" value=\"Valider\"/>";
							echo "</form>";

						}else{
							echo "<p>Vous n''&ecirc;tes pas inscrit dans ce jeu</p>";
						}
					}
					
				}else{
					echo "<p>Vous n''&ecirc;tes pas inscrit dans ce jeu</p>";
				}
				
				
				
			}else{
				echo "<p>Vous n'&ecirc;tes pas inscrits dans ce jeu</p>";
			}
			
		
				
		?>
		</div>
	</div>
	<?php
		pied_page();
	?>
</body>
</html>
